﻿// 9. Write a method that return the maximal element in a portion of array of integers starting at given index. 
// Using it write another method that sorts an array in ascending / descending order.

using System;

class FindMaxElementInPortion
{
    static void Main()
    {
        Console.Write("Enter the length of the array : ");
        int length = int.Parse(Console.ReadLine());

        if (length < 0)
        {
            Console.WriteLine("Array with negative length cannot exist.");
            return;
        }
        else
        {
            Console.WriteLine("Enter values for the array : ");
            int[] array = new int[length];
            for (int i = 0; i < length; i++)
            {
                Console.Write("array[{0}] = ", i);
                array[i] = int.Parse(Console.ReadLine());
            }

            Console.Write("Enter start index of the portion : ");
            int start = int.Parse(Console.ReadLine());
            Console.Write("Enter end index of the portion : ");
            int end = int.Parse(Console.ReadLine());

            int maxNumber = GetMaxNumber(array, start, end);
            
            Console.WriteLine( "The max number in the chosen portion of the array is {0}.", maxNumber);
            Console.WriteLine();

            int[] ascSortedArray = new int[length];
            ascSortedArray = AscendingSortArray(array);
            Console.WriteLine("The ascending sorted array : ");
            PrintArray(ascSortedArray);
            Console.WriteLine();

            int[] descSortedArray = new int[length];
            descSortedArray = DescendingSortArray(array);
            Console.WriteLine("The descending sorted array : ");
            PrintArray(descSortedArray);
            Console.WriteLine();
        }
    }

    static int GetMaxNumber (int[] array, int start, int end)
    {
        int maxNumber = int.MinValue;

        for (int i = start; i <= end; i++)
        {
            if (array[i] > maxNumber)
            {
                maxNumber = array[i];
            }
        }

        return maxNumber;
    }

    static int[] AscendingSortArray(int[] array)
    {
        for (int i = 0; i < array.Length - 1; i++)
        {
            int startIndex = i;
            for (int j = i + 1; j < array.Length; j++)
            {
                if (array[startIndex] > array[j])
                {
                    startIndex = j;
                }
            }
            int temporaryValue = array[startIndex];
            array[startIndex] = array[i];
            array[i] = temporaryValue;
        }

        return array;
    }

    static int[] DescendingSortArray(int[] array)
    {
        for (int i = 0; i < array.Length - 1; i++)
        {
            int startIndex = i;
            for (int j = i + 1; j < array.Length; j++)
            {
                if (array[startIndex] < array[j])
                {
                    startIndex = j;
                }
            }
            int temporaryValue = array[startIndex];
            array[startIndex] = array[i];
            array[i] = temporaryValue;
        }

        return array;
    }

    static void PrintArray(int[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            Console.WriteLine("array[{0}] = {1}", i, array[i]);
        }
    }

}
